﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LocSiteBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILocSiteDA LocSiteDA = DataAccess.CreateLocSiteDA();
        private PubParasInfo pubParasInfo;

        public LocSiteBLL()
        {
            this.pubParasInfo = null;
        }

        public LocSiteBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LocSiteBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LocSiteInfo> locSiteInfos = new List<LocSiteInfo>();
                LocSiteInfo locSiteInfo = null;
                foreach (string id in IDS)
                {
                    locSiteInfo = LocSiteDA.GetLocSiteByID(dataBase, tran, id, this.dataBaseID[0]);
                    locSiteInfos.Add(locSiteInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (LocSiteDA.DeleteLocSite(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LocSiteInfo>(dataBase, tran, locSiteInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOC_SITE", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return LocSiteDA.GetAllLocSite(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return LocSiteDA.GetLocSiteByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            LocSiteQueryEntity LocSiteQuery = obj as LocSiteQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(LocSiteQuery.LocSiteID))
            {
                sqlWhere.Add(" LOC_SITE_ID " + LocSiteQuery.LocSiteID);
            }
            if (!string.IsNullOrEmpty(LocSiteQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + LocSiteQuery.Descr);
            }
            if (!string.IsNullOrEmpty(LocSiteQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + LocSiteQuery.IsActive);
            }
            return LocSiteDA.GetLocSiteByQueryList(sqlWhere, LocSiteQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string locSiteID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocSiteInfo locSiteInfo = obj as LocSiteInfo;
                if (LocSiteDA.CheckLocSiteIDUnique(dataBase, tran, locSiteInfo.LocSiteID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2540_001", this.pubParasInfo.UserLanguage, new List<string> { locSiteInfo.LocSiteID });
                }
                locSiteInfo.UpdatedBy = locSiteInfo.CreatedBy;
                locSiteInfo.CreatedDate = DateTime.Now;
                locSiteInfo.UpdatedDate = DateTime.Now;
                if (LocSiteDA.InsertLocSite(dataBase, tran, locSiteInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocSiteInfo>(dataBase, tran, null, locSiteInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LOC_SITE", this.dataBaseID[0]);
                    locSiteID = locSiteInfo.LocSiteID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locSiteID;
        }

        public string Update(object obj)
        {
            string locSiteID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocSiteInfo locSiteInfo = obj as LocSiteInfo;
                locSiteInfo.UpdatedDate = DateTime.Now;
                LocSiteInfo oldLocSitInfo = LocSiteDA.GetLocSiteByID(dataBase, tran, locSiteInfo.LocSiteID, this.dataBaseID[0]);
                if (LocSiteDA.UpdateLocSite(dataBase, tran, locSiteInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocSiteInfo>(dataBase, tran, oldLocSitInfo, locSiteInfo, AuditAction.Update, this.pubParasInfo.UserID, "LOC_SITE", this.dataBaseID);
                    locSiteID = locSiteInfo.LocSiteID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locSiteID;
        }
    }
}

